Expression And Filter

64

Expression And Filter -

public async Task<UserData> GetUserByName(string name)

​    {

​      BinaryExpression filter = null;
​      var data = new Dictionary<string, string>();
​      data.Add("name", name);
​      data.Add("technology", ".Net");
​      var userData = Expression.Parameter(typeof(UserData));
​      foreach (var toProcess in data)
​      {
​        var memberExpression = Expression.PropertyOrField(userData, toProcess.Key);
​        var constantExpression = Expression.Constant(toProcess.Value);
​        var binaryExpression = Expression.Equal(memberExpression, constantExpression); 
​        filter = (filter != null) ? Expression.And(filter, binaryExpression) : binaryExpression;

​      } 
​      Expression<Func<UserData, bool>> lambda = Expression.Lambda<Func<UserData, bool>>(filter, userData);
​      return await _dbContext.UserData.Where(lambda).FirstOrDefaultAsync();
​    }

Comments

Submit
0 Comments